var modePartnerSelector = 0, modeCatsSelector = 0;
var chechedPartners = new Array();

function savePartners() {
    chechedPartners = new Array();
    var i = 0;
    $('.listPartners:checked').each(function() {
        chechedPartners[i] = $(this).val();
        i++;
    });
}

function checkPartners() {
    $('.listPartners').each(function() {
        for(a in chechedPartners) {
            if($(this).val() == chechedPartners[a]) $(this).attr('checked', true);
        }
    });
}

function selectPartners() {
    $(".listPartners").each(function() {
        $(this).attr('checked', Boolean(!modePartnerSelector));
    });
    modePartnerSelector = modePartnerSelector ? 0 : 1 ;
}

function selectCats() {
    $(".catsList").each(function() {
        $(this).attr('checked', Boolean(!modeCatsSelector));
    });
    modeCatsSelector = modeCatsSelector ? 0 : 1 ;
}

function loadPartners() {
    var catsid = '';
    $('.catsList:checked').each(function(){
        catsid += $(this).val() + ','; 
    });
    if(catsid != '') {
        $('#partners').load('/ajax/getPartnersList', {'catsid':catsid},function() {
            checkPartners();
            $('#selectPartners').click(function() {
                selectPartners();
            });
        });
    } else $('#partners').empty();
}

$(document).ready(
    function() {
        $('.catsList').click(function() {
            savePartners();
            loadPartners();
        });

        $('#selectCats').click(function(){
            selectCats();
            loadPartners();
        });
    }
);    